package com.nc.cloud.admin.rpc.service;

/**
 * @Description
 * @ClassName PermissionService
 * @Author NC—Cloud
 * @date 2020.04.03 14:17
 */

import com.nc.cloud.admin.biz.MenuBiz;
import com.nc.cloud.admin.biz.UserBiz;
import com.nc.cloud.admin.biz.UserRoleBiz;
import com.nc.cloud.admin.entity.Menu;
import com.nc.cloud.admin.entity.User;
import com.nc.cloud.api.vo.user.UserInfo;
import com.nc.cloud.common.constant.UserConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PermissionService {

    //用户Biz
    @Autowired
    private UserBiz userBiz;

    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT);

    public UserInfo getUserByUsername(String username) {
        UserInfo info = new UserInfo();
        User user = userBiz.getUserByUserName(username);
        BeanUtils.copyProperties(user, info);
        info.setUid(user.getUid());
        return info;
    }

    public UserInfo validate(String username,String password){
        UserInfo info = new UserInfo();
        User user = userBiz.getUserByUserName(username);
        try {
            String codestr =  encoder.encode(password);
            boolean compari = codestr.equals(user.getPwd());
            if (encoder.matches(password, user.getPwd())) {
                BeanUtils.copyProperties(user, info);
                info.setUid(user.getUid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }


}
